<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>回收站地图</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">
    <!-- 先加载百度地图API -->
    <script type="text/javascript" th:src="@{https://api.map.baidu.com/api?v=3.0&ak=${@environment.getProperty('baidu.map.apiKey')}}"></script>
    <style>
        #map-container {
            width: 100%;
            height: 600px;
        }
        .info-window {
            padding: 10px;
        }
        .info-window h4 {
            margin: 0 0 5px 0;
        }
        .info-window p {
            margin: 5px 0;
        }
        .btn {
            display: inline-block;
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 3px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div id="map-container"></div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 确保map-component.js在百度地图API之后加载 -->
    <script th:src="@{/js/map-component.js}"></script>
    <script th:inline="javascript">
        let mapComponent;
        
        // 初始化地图
        document.addEventListener('DOMContentLoaded', async () => {
            try {
                mapComponent = new BaiduMapComponent('map-container', {
                    enableGeolocation: true,
                    onMapInitialized: async function(map) {
                        console.log('地图初始化完成');
                        
                        try {
                            // 获取回收站列表
                            const response = await fetch('/api/stations');
                            if (!response.ok) {
                                throw new Error('获取站点数据失败');
                            }
                            const data = await response.json();
                            const stations = Array.isArray(data) ? data : (data.content || []);
                            
                            stations.forEach(station => {
                                if (station.latitude && station.longitude) {
                                    const point = new BMapGL.Point(station.longitude, station.latitude);
                                    const marker = new BMapGL.Marker(point);
                                    map.addOverlay(marker);
                                    
                                    // 创建信息窗口
                                    const content = `
                                        <div class="info-window">
                                            <h4>${station.name || '未命名站点'}</h4>
                                            <p><strong>地址：</strong>${station.address || '暂无地址'}</p>
                                            <p><strong>电话：</strong>${station.contactPhone || '暂无电话'}</p>
                                            <p><strong>营业时间：</strong>${station.businessHours || '暂无信息'}</p>
                                            <p><strong>可回收物品：</strong>${station.acceptableItems || '暂无信息'}</p>
                                            <a href="javascript:void(0)" onclick="navigate(${station.latitude}, ${station.longitude})" class="btn">导航</a>
                                        </div>
                                    `;
                                    
                                    const infoWindow = new BMapGL.InfoWindow(content);
                                    marker.addEventListener('click', function() {
                                        map.openInfoWindow(infoWindow, point);
                                    });
                                }
                            });
                        } catch (error) {
                            console.error('加载站点数据失败:', error);
                        }
                    }
                });
                
                await mapComponent.init();
                
            } catch (error) {
                console.error('地图初始化失败:', error);
            }
        });
        
        // 导航功能
        async function navigate(endLat, endLng) {
            try {
                const map = mapComponent.getMap();
                const currentPoint = await mapComponent.enableGeolocation();
                
                const driving = new BMapGL.DrivingRoute(map, {
                    renderOptions: {
                        map: map,
                        autoViewport: true
                    }
                });
                
                const endPoint = new BMapGL.Point(endLng, endLat);
                driving.search(currentPoint, endPoint);
            } catch (error) {
                console.error('导航失败:', error);
            }
        }
    </script>
</body>
</html> 